/*
 * Created by JFormDesigner on Tue Jul 02 10:33:54 CST 2024
 */

package com.vict.gui;

import com.vict.GuiApplication;
import com.vict.bean.DbConfig;
import com.vict.config.SettingConfig;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
@Slf4j
public class DbEditGui extends JFrame {
    DefaultTableModel defaultTableModel;
    public DbEditGui() {
        initComponents();
        // this.dbConfigList.isCellEditable(false);
        this.defaultTableModel = new DefaultTableModel(
                new Object[]{
                        "name",
                        "url",
                        "username",
                        "password"
                }, 0);
        this.dbConfigTable.setModel(defaultTableModel);
        reloadDbConfig();
    }

    public void update(){
        this.reloadDbConfig();
    }

    private void reloadDbConfig(){
        List<DbConfig> dbConfigList = SettingConfig.getDbConfigList();
        dbConfigList = Optional.ofNullable(dbConfigList).orElse(new ArrayList<>());

        this.defaultTableModel.setRowCount(dbConfigList.size());

        Vector<Vector> dataVector = defaultTableModel.getDataVector();
        dataVector.clear();

        for(DbConfig dbConfig : Optional.ofNullable(dbConfigList).orElse(new ArrayList<>())){
            Vector vector = new Vector();
            vector.add(dbConfig.getName());
            vector.add(dbConfig.getUrl());
            vector.add(dbConfig.getUsername());
            vector.add(dbConfig.getPassword());
            dataVector.add(vector);
        }
    }

    private void addAction(ActionEvent e) {
        GuiApplication.guiApplication.getAddDbConfigGui().setVisible(true);
        this.setVisible(false);
    }

    private void delBtnAction(ActionEvent e) {
        int selectedRow = this.dbConfigTable.getSelectedRow();
        if(selectedRow == -1){
            JOptionPane.showMessageDialog(this, "请选择要删除的数据");
        }else{
            int flag = JOptionPane.showConfirmDialog(this, "是否确认删除?",
                    "提示!", JOptionPane.YES_NO_OPTION,
                    JOptionPane.INFORMATION_MESSAGE);
            if(JOptionPane.YES_OPTION == flag){
                List<DbConfig> dbConfigList = SettingConfig.getDbConfigList();
                dbConfigList.remove(selectedRow);
                SettingConfig.saveDbConfig(dbConfigList);
                this.defaultTableModel.removeRow(selectedRow);
            }
        }
    }

    private void editBtnAction(ActionEvent e) {
        int selectedRow = this.dbConfigTable.getSelectedRow();
        if(selectedRow == -1){
            JOptionPane.showMessageDialog(this, "请选择要修改的数据");
        }else{
            GuiApplication.guiApplication.getEditDbConfigGui().setIndex(selectedRow);
            GuiApplication.guiApplication.getEditDbConfigGui().update();
            GuiApplication.guiApplication.getEditDbConfigGui().setVisible(true);
            this.setVisible(false);
        }
    }

    private void thisWindowClosing(WindowEvent e) {
        GuiApplication.guiApplication.getMainWindow().update();
        GuiApplication.guiApplication.getMainWindow().setVisible(true);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        dbConfigTable = new JTable();
        delBtn = new JButton();
        editBtn = new JButton();

        //======== this ========
        setTitle("\u6570\u636e\u5e93[\u914d\u7f6e");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button1 ----
        button1.setText("\u65b0\u589e");
        button1.addActionListener(e -> addAction(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(400, 15), button1.getPreferredSize()));

        //======== scrollPane1 ========
        {

            //---- dbConfigTable ----
            dbConfigTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            scrollPane1.setViewportView(dbConfigTable);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(30, 10, 325, 235);

        //---- delBtn ----
        delBtn.setText("\u5220\u9664");
        delBtn.addActionListener(e -> delBtnAction(e));
        contentPane.add(delBtn);
        delBtn.setBounds(new Rectangle(new Point(400, 95), delBtn.getPreferredSize()));

        //---- editBtn ----
        editBtn.setText("\u7f16\u8f91");
        editBtn.addActionListener(e -> editBtnAction(e));
        contentPane.add(editBtn);
        editBtn.setBounds(new Rectangle(new Point(400, 55), editBtn.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(515, 300));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable dbConfigTable;
    private JButton delBtn;
    private JButton editBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
